package me.zhengjie.nettydemo.mapper;

import lombok.RequiredArgsConstructor;
import me.zhengjie.pojo.entity.Lift;
import me.zhengjie.nettydemo.lift.LiftProtocolFrameContent;
import me.zhengjie.nettydemo.lift.LiftStatusVo;

import me.zhengjie.sqlservilce.LiftSqlService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@RequiredArgsConstructor
public class LiftNettyMapperImpl implements LiftNettyMapper{
    private static final Logger log = LoggerFactory.getLogger("lift");

    private final LiftSqlService liftSqlService;

    @Qualifier("myRedisTemplateInt")
    private final RedisTemplate<String, Integer> redisTemplateInt;

    @Qualifier("myRedisTemplate")
    private final RedisTemplate<String, LiftStatusVo> redisTemplateLift;


    @Override
    public List<Lift> selectAll() {
        List<Lift> liftList = liftSqlService.selectAll();
        return liftList;
    }

    @Override
    public void storageCallLiftFrame(String liftCode, int resp) {
//        map.put(liftCode + LiftProtocolFrameContent.CALL_LIFT_FRAME, resp);
        redisTemplateInt.opsForValue().set(liftCode + LiftProtocolFrameContent.CALL_LIFT_FRAME, resp, 25, TimeUnit.SECONDS);
        log.info("储存呼叫电梯的返回值,电梯为:{},resp为:{},获取redis:{}",liftCode,resp,redisTemplateInt.opsForValue().get(liftCode + LiftProtocolFrameContent.CALL_LIFT_FRAME));
    }

    @Override
    public void storage0penDoor(String liftCode,int resp) {
        redisTemplateInt.opsForValue().set(liftCode + LiftProtocolFrameContent.OPEN_DOOR, resp, 25, TimeUnit.SECONDS);
        log.info("储存关门至某层返回值,电梯为:{},resp为:{},获取redis:{}",liftCode,resp,redisTemplateInt.opsForValue().get(liftCode + LiftProtocolFrameContent.OPEN_DOOR));
    }

    @Override
    public void storageCloseDoorFrame(String liftCode,int resp) {
        redisTemplateInt.opsForValue().set(liftCode + LiftProtocolFrameContent.CLOSE_DOOR_FRAME, resp, 10, TimeUnit.SECONDS);
        log.info("储存关门返回值,电梯为:{},resp为:{},获取redis:{}",liftCode,resp,redisTemplateInt.opsForValue().get(liftCode + LiftProtocolFrameContent.CLOSE_DOOR_FRAME));
    }

    @Override
    public void storageLiftStatusFrame(String liftCode, LiftStatusVo resp) {
        redisTemplateLift.opsForValue().set(liftCode + LiftProtocolFrameContent.LIFT_STATUS_FRAME,resp);
    }

    @Override
    public LiftStatusVo findLiftInfo(String liftCode) {
        return redisTemplateLift.opsForValue().get(liftCode + LiftProtocolFrameContent.LIFT_STATUS_FRAME);
    }

    @Override
    public Integer getStorageCallLiftFrame(String liftCode) {
        return redisTemplateInt.opsForValue().get(liftCode + LiftProtocolFrameContent.CALL_LIFT_FRAME);
    }

    @Override
    public Integer get0penDoor(String liftCode) {
        return redisTemplateInt.opsForValue().get(liftCode + LiftProtocolFrameContent.OPEN_DOOR);

    }

    @Override
    public Integer getStorageCloseDoorFrame(String liftCode) {
        return redisTemplateInt.opsForValue().get(liftCode + LiftProtocolFrameContent.CLOSE_DOOR_FRAME);
    }


}
